#include "stdafx.h"
#include "HtmlProcess.h"
#include <stdio.h>
#include <iostream>
#include <string.h>

// vector<Comment *> * allComments; 

char *formatComment(Comment *_in) {
	int noerr;
	static char format_buffer[1024];
	char state = ((*(_in->getAllComments()))[0])[0];
	((*(_in->getAllComments()))[0]).erase(0, 1);
	if (state == '1') {			// @author
		noerr = sprintf(format_buffer, "<p>@author<br>");
		for (int i = 0; i < (int)(*(_in->getAllComments())).size(); i++) {
			const char *temp = (*(_in->getAllComments()))[i].c_str();
			strcat (format_buffer, temp);
		}
		strcat(format_buffer, "\n</p>");
		return format_buffer;
	}
	else if (state == '2') {	// @desc
		noerr = sprintf(format_buffer, "<p>@description<br>");
		for (int i = 0; i < (int)(*(_in->getAllComments())).size(); i++) {
			const char *temp = (*(_in->getAllComments()))[i].c_str();
			strcat (format_buffer, temp);
		}
		strcat(format_buffer, "\n</p>");
		return format_buffer;
	}
	else if (state == '3') {	// @date
		noerr = sprintf(format_buffer, "<p>@date<br>");
		for (int i = 0; i < (int)(*(_in->getAllComments())).size(); i++) {
			const char *temp = (*(_in->getAllComments()))[i].c_str();
			strcat (format_buffer, temp);
		}
		strcat(format_buffer, "\n</p>");
		return format_buffer;
	}
	else if (state == '4') {	// @todo in red
		noerr = sprintf(format_buffer, "<p><font color = \"red\">@todo<br>");
		for (int i = 0; i < (int)(*(_in->getAllComments())).size(); i++) {
			const char *temp = (*(_in->getAllComments()))[i].c_str();
			strcat (format_buffer, temp);
		}
		strcat(format_buffer, "\n</font>\n</p>");
		return format_buffer;
	}
	else {
		// std::cout << "Unrecognizable format type.\n\n";
		return NULL;
	}
}

bool createDoc(vector<Comment *> * _invec) {
	if (_invec == NULL) {
		// std::cout << "Null pointer to vector of Comments.\n";
		return false;
	}
	else if (_invec->empty()) {
		// std::cout << "Empty vector of Comments.\n";
		return false;
	}
	else {
		FILE *current_file;
		string current_name = (*_invec)[0]->getfileName();
		string previous_name = "";
		bool start = true;
		char buffer[1024];
		for (int i = 0; i < (int)_invec->size(); i++) {
			if ((*_invec)[i]->getfileName() != previous_name) {
				if (!start) {
					strcat(buffer, "</body>\n</html>\n");
					previous_name.append(".html");
					current_file = fopen(previous_name.c_str(), "w");
					fprintf(current_file, buffer);
					fclose(current_file);
		
					sprintf(buffer, "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n<title>\nCPlusPlus Doc for ");
					strcat(buffer, (*_invec)[i]->getfileName().c_str());
					strcat(buffer, "\n</title>\n</head>\n<body>\n");
					strcat(buffer, formatComment((*_invec)[i]));
					previous_name = (*_invec)[i]->getfileName();
				}
				else {
					sprintf(buffer, "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n\"http://www.w3.org/TR/html4/loose.dtd\">\n<html>\n<head>\n<title>\nCPlusPlus Doc for ");
					strcat(buffer, (*_invec)[i]->getfileName().c_str());
					strcat(buffer, "\n</title>\n</head>\n<body>\n");
					strcat(buffer, formatComment((*_invec)[i]));
					previous_name = (*_invec)[i]->getfileName().c_str();
					start = false;
				}
			}
			else if ((*_invec)[i]->getfileName() == previous_name) {
				strcat(buffer, formatComment((*_invec)[i]));
				previous_name = (*_invec)[i]->getfileName();
			}
		}
		strcat(buffer, "</body>\n</html>\n");
		previous_name.append(".html");
		current_file = fopen(previous_name.c_str(), "w");
		fprintf(current_file, buffer);
		fclose(current_file);
		return true;
	} 
}
